const express = require('express')

//创建web服务器
const app = express();

//跨域
const cors = require('cors');
app.use(cors());
 
//配置解析 application/json 格式数据的内置中间件
app.use(express.json());
//配置解析 application/x-www-from-urlencoded 格式数据的内置中间件
app.use(express.urlencoded({ extended: false }));


//导入配置文件
const config = require('./config/config');
//导入解析token中间件
const expressJWT = require('express-jwt');


//配置
app.use(expressJWT.expressjwt({algorithms:['HS256'],secret: config.jwtSecretKey}).unless({path: [/^\/(?!api$|pic$)/]}));


//在路由之前封装 res.cc 函数，用来处理错误
app.use((req,res,next) => {
    res.cc = function(err, code = 400){
        res.send({
            code,
            message:err instanceof Error ? err.message : err
        })
    }
    next()
})

//对外提供静态资源
app.use('/pic', express.static('./uploads'));

//导入用户路由并使用路由模块
const userRouter = require('./router/user');
app.use('/user', userRouter);

//用户注册登录 路由模块
const userLoginRouter = require('./router/userLogin');
app.use('/api', userLoginRouter);

//商品展示 路由模块 无需登录
const goodsShowRouter = require('./router/goodsShow');
app.use('/api', goodsShowRouter);

//商品路由模块 需要登录
const goodsRouter = require('./router/goods');
app.use('/goods', goodsRouter);

//记录路由模块
const recordsRouter = require('./router/records');
app.use('/records',recordsRouter);

//评论
const commentRouter = require('./router/comment'); 
app.use('/comment',commentRouter);

//收藏
const collectRouter = require('./router/collect');
app.use('/collect',collectRouter);

//api路由
const apiRouter = require('./router/api');
app.use('/api',apiRouter);

//banner路由
const bannerRouter = require('./router/banner');
app.use('/banner',bannerRouter);

//setting路由
const settingRouter = require('./router/setting');
app.use('/setting',settingRouter);

//category路由
const categoryRouter = require('./router/category');
app.use('/category',categoryRouter);


// 定义错误级别的中间件
app.use((err,req,res,next) => {
    console.log(err);
    //token验证失败的错误
    if(err.name === 'UnauthorizedError') return res.send({code:400,message:'Identity authentication failure'})

    //未知错误
    res.send({code:400,message:'未知错误'})
})

//调用app.listen(端口号，启动成功后的回调函数)，启动服务器
app.listen(3000, () => {
    console.log('express server running at http://127.0.0.1:3000');
})
